//
//  NewFeatureViewController.m
//  weibo
//
//  Created by apple on 13-8-28.
//  Copyright (c) 2013年 itcast. All rights reserved.
//

#import "NewFeatureViewController.h"
#import "MainTabBarViewController.h"
#import "Masonry.h"
#define kCount 3

@interface NewFeatureViewController ()
{
    UIPageControl *_pageControl;
    UIButton *_skipBtn;
}
@end

@implementation NewFeatureViewController

#pragma mark 自定义控制器的view
//- (void)loadView
//{
//    UIImageView *imageView = [[UIImageView alloc] init];
//    imageView.frame = [UIScreen mainScreen].bounds;
//    imageView.image = [UIImage imageNamed:@"new_feature_background.png"];
//    imageView.userInteractionEnabled = YES;
//    self.view = imageView;
//}

#pragma mark view加载完毕
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    CGSize viewSize = self.view.bounds.size;
    
    // 1.加载UIScrollView
    UIScrollView *scrollView = [[UIScrollView alloc] init];
    scrollView.frame = self.view.bounds;
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.pagingEnabled = YES;
    scrollView.contentSize = CGSizeMake(kCount * viewSize.width, 0);
    scrollView.delegate = self;
    [self.view addSubview:scrollView];
    
    // 2.添加UIImageView
    for (int i = 0; i<kCount; i++) {
        [self addImageViewAtIndex:i inView:scrollView];
    }
    
    
    //2.1 跳过按钮
    [self addBtnInView];
    
    // 3.加载UIPageControl
//    UIPageControl *pageControl = [[UIPageControl alloc] init];
//    pageControl.center = CGPointMake(viewSize.width * 0.5, viewSize.height * 0.95);
//    pageControl.bounds = CGRectMake(0, 0, 100, 0);
//    pageControl.numberOfPages = kCount;
//    pageControl.pageIndicatorTintColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"new_feature_pagecontrol_point.png"]];
//    pageControl.currentPageIndicatorTintColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"new_feature_pagecontrol_checked_point.png"]];
//    pageControl.userInteractionEnabled = NO;
//    [self.view addSubview:pageControl];
//    _pageControl = pageControl;
    
}

#pragma mark 添加scrollview里面的imageview
- (void)addImageViewAtIndex:(int)index inView:(UIView *)view
{
    CGSize viewSize = self.view.frame.size;
    // 1.创建imageview
    UIImageView *imageView = [[UIImageView alloc] init];
    imageView.frame = (CGRect){{index * viewSize.width, 0} , viewSize};
    //imageView.frame = CGRectMake(index * viewSize.width, 0, viewSize.width, viewSize.height);
    
    // 2.设置图片
    NSString *name = [NSString stringWithFormat:@"new_feature_%d.png", index + 1];
    imageView.image = [UIImage imageNamed:name];
    
    // 3.添加
    [view addSubview:imageView];
    
    
    // 4.如果是最后一张图片，添加2个按钮（分享、开始）
    if (index == kCount - 1) {
        [self addLastBtnInView:imageView];
    }

}

- (void)addLastBtnInView:(UIView *)view{
    // 1.1.创建
    UIButton *start = [UIButton buttonWithType:UIButtonTypeCustom];
    
    [view addSubview:start];
    view.userInteractionEnabled = YES;
//    [start mas_makeConstraints:^(MASConstraintMaker *make) {
//        //与中心点作参考 约束
//        // 中心点与父试图中心点一致
//         make.center.equalTo(view);
//        make
//        //大小
//        make.size.mas_equalTo(CGSizeMake(250, 55));
//
//    }];
//    if (iPhone4) {
//        
//        [start setFrame:CGRectMake((Screen_Width - 250)/2, Screen_Height - 115, 250, 55)];
//        
//    }else{
//        
//        [start setFrame:CGRectMake((Screen_Width - 250)/2, Screen_Height - 145, 250, 55)];
//    }
    [start setFrame:CGRectMake(0, 0, Screen_Width, Screen_Height)];
    // 1.2.设置背景图片
    //    CGSize startSize = [start setAllStateBg:@"new_feature_finish_button.png"];
//    UIImage *startNormal = [UIImage imageNamed:@"new_feature_last_skip_button.png"];
    //    UIImage *startHighlighted = [UIImage imageNamed:@"new_feature_skip_button_highlighted.png"];
//    [start setBackgroundImage:startNormal forState:UIControlStateNormal];
    //    [start setBackgroundImage:startHighlighted forState:UIControlStateHighlighted];
    
    // 1.4.监听
    [start addTarget:self action:@selector(start) forControlEvents:UIControlEventTouchUpInside];
}

#pragma mark -- 跳过
- (void)addBtnInView
{
    
    /*
     1.开始
     */
    // 1.1.创建
    UIButton *start = [UIButton buttonWithType:UIButtonTypeCustom];
   
    [self.view addSubview:start];
    
//    [start mas_makeConstraints:^(MASConstraintMaker *make) {
//        //在 父试 图中 上边的距离
//        make.top.mas_equalTo(Screen_Width - 60);
//        make.trailing.mas_equalTo(-20);
//        //大小
//        make.size.mas_equalTo(CGSizeMake(72, 31));
//    }];
    
    
    [start setFrame:CGRectMake((Screen_Width - 110)/2, Screen_Height - 90, 110, 36)];
//    
    // 1.2.设置背景图片
//    CGSize startSize = [start setAllStateBg:@"new_feature_finish_button.png"];
    UIImage *startNormal = [UIImage imageNamed:@"new_feature_skip_button.png"];
//    UIImage *startHighlighted = [UIImage imageNamed:@"new_feature_skip_button_highlighted.png"];
    [start setBackgroundImage:startNormal forState:UIControlStateNormal];
//    [start setBackgroundImage:startHighlighted forState:UIControlStateHighlighted];
   
    // 1.4.监听
    [start addTarget:self action:@selector(start) forControlEvents:UIControlEventTouchUpInside];
    _skipBtn = start;
}

// 切换按钮图片可以通过切换按钮的状态来完成
/*
 normal：普通
 highlighted ： 高亮（人为长按的时候）
 disabled ：失效（通过代码控制）
 selected : 选中 （通过代码控制）
 */


#pragma mark 开始
- (void)start
{

//    // 0.显示状态栏
    [UIApplication sharedApplication].statusBarHidden = NO;
    
    
    //加载Storyboard
    UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    
    //从StoryBoard 中加载 指定标识 的控制器
    MainTabBarViewController *mtb =  [sb instantiateInitialViewController];
    
//    [self.navigationController pushViewController:vcName animated:YES];
//    // 1.创建主界面-MainViewController
//    MainViewController *main = [[MainViewController alloc] init];
//    
//    // [UIApplication sharedApplication].keyWindow
//    // 2.切换窗口的根控制器（自动销毁新特性控制器）
    self.view.window.rootViewController = mtb;
//    // [self presentViewController:main animated:YES completion:nil];
}

#pragma mark 滚动代理
#pragma mark 减速完毕就会调用（scrollview静止）
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    
//    _pageControl.currentPage = scrollView.contentOffset.x / scrollView.frame.size.width;
    
//    if (_pageControl.currentPage == kCount - 1) {
//        _skipBtn.hidden = YES;
//    }else{
//        _skipBtn.hidden = NO;
//    }
    
}
@end
